﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;
using WhsFacebook.Whs;

namespace WhsFacebook.Net.Web.Facebook
{
    public partial class Storage : CanvasFBMLBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var db = new Database.WhsFacebookEntities())
            {
                var instanceId = Properties.Settings.Default.OverallInstanceId;

                // storage data
                {
                    // link image
                    this.storageChart.Src = string.Format(
                        "{0}?id={1}",
                        this.ResolveUrlAbsolute("~/Facebook/Handlers/StorageChartJpeg.aspx"),
                        instanceId
                        );

                    // storage details
                    var i = db.UserStorageInfosLatests.Where(o => o.InstanceId == instanceId).FirstOrDefault();

                    if (i == null)
                    {
                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "ERROR", "No data has yet been submitted. Please make sure the add-ins is running on your WHS by opening the 'Facebook' tab in the WHS Console settings tab.");
                    }
                    else
                    {
                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Storage", string.Format(
                            "{0} used, {1} free, {2} total",
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageUsedSpace),
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageFreeSpace),
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "System", string.Format(
                            "{0} operating system, {1} app folders",
                            WhsFacebookHelpers.GetNiceFileSize(i.OperatingSystemSize),
                            WhsFacebookHelpers.GetNiceFileSize(i.AppFoldersSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Backups", string.Format(
                            "{0} client computer backups",
                            WhsFacebookHelpers.GetNiceFileSize(i.BackupSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Files", string.Format(
                            "{0} shared folders, {1} duplicates",
                            WhsFacebookHelpers.GetNiceFileSize(i.SharedFoldersSize),
                            WhsFacebookHelpers.GetNiceFileSize(i.SharedFoldersDupSize)
                            ));
                    }
                }
            }

        }
    }
}